package com.pj.project.update_log;

import java.util.List;

import com.pj.project.update_package.PackageInfo;
import com.pj.project4sp.SP;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pj.utils.sg.*;

import static com.pj.project.update_package.UpdatePackageService.pageMap;

/**
 * Service: update_log -- 升级记录
 * @author xxxxxx
 */
@Service
public class UpdateLogService {

	/** 底层 Mapper 对象 */
	@Autowired
	UpdateLogMapper updateLogMapper;

	/** 增 */
	int add(UpdateLog u){
		return updateLogMapper.add(u);
	}

	/** 删 */
	int delete(Integer id){
		return updateLogMapper.delete(id);
	}

	/** 改 */
	public int update(UpdateLog u){
		return updateLogMapper.update(u);
	}

	/** 查 */
	UpdateLog getById(Integer id){
		return updateLogMapper.getById(id);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */
	List<UpdateLog> getList(SoMap so) {

		List<UpdateLog> list = updateLogMapper.getList(so);
		for (UpdateLog updateLog : list) {
			PackageInfo packageInfo = pageMap.get(updateLog.deviceCode);
			if(null != packageInfo){
				if(updateLog.getId().equals(packageInfo.getLogInfo().getId())){
					updateLog.setNum(packageInfo.getLogInfo().getNum());
				}
			}
		}
		return list;
	}


	public UpdateLog addUpdateLog(PackageInfo packageInfo){
		UpdateLog log = new UpdateLog();
		log.setFileName(packageInfo.getFileName());
		log.setDeviceCode(packageInfo.getTopic());
		log.setStatus(1);
		log.setMaxSize(packageInfo.getMaxSize());
		log.setDynamicSize(packageInfo.getDynamicSize());

		updateLogMapper.add(log);
		packageInfo.setLogInfo(log);
		return log;
	}


}
